package cn.dslcode.spring.session.redis.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.HttpSessionStrategy;

/**
 * Created by dongsilin on 2018/1/18.
 */
@Slf4j
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 120)
public class RedisSessionConfig {

    @Autowired
    private CommonHttpSessionStrategy sessionStrategy;

    /**
     * 自定义sessionRedisTemplate的hashValueSerializer
     * @return
     */
    @Bean("springSessionDefaultRedisSerializer")
    public RedisSerializer springSessionDefaultRedisSerializer(){
        return new SessionRedisJsonSerializer();
    }

    /**
     * 自定义HttpSessionStrategy
     * @return
     */
    @Bean
    public HttpSessionStrategy httpSessionStrategy() {
        sessionStrategy.sessionMaxAge = RedisSessionConfig.class.getAnnotation(EnableRedisHttpSession.class).maxInactiveIntervalInSeconds();
        return sessionStrategy;
    }

}
